package com.csw.flink.sink

import java.util.concurrent.TimeUnit

import org.apache.flink.api.common.serialization.SimpleStringEncoder
import org.apache.flink.core.fs.Path
import org.apache.flink.streaming.api.functions.sink.filesystem.StreamingFileSink
import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.DefaultRollingPolicy
import org.apache.flink.streaming.api.scala._

object Demo01Sink {
  def main(args: Array[String]): Unit = {

    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    env.setParallelism(1)

    val ds: DataStream[String] = env.socketTextStream("master",8888)

    /**
      *
      * 打印出来
      */

//    ds.print()

    /**
      *
      * 写入文件
      */

    //已经过时了，现在使用StreamingFileSink
//    ds.writeAsText("flink/data/out")


    val fileSink: StreamingFileSink[String] = StreamingFileSink
      .forRowFormat(new Path("flink/data/out1"), new SimpleStringEncoder[String]("UTF-8"))
      .withRollingPolicy(
        DefaultRollingPolicy.builder()
          .withRolloverInterval(TimeUnit.MINUTES.toMillis(15))
          .withInactivityInterval(TimeUnit.MINUTES.toMillis(5))
          .withMaxPartSize(1024 * 1024 * 1024)
          .build())
      .build()

    ds.addSink(fileSink)

    env.execute()
  }
}
